package com.prd.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.prd.enums.BizCodeEnum;
import com.prd.enums.LoginUserTypeEnum;
import com.prd.mapper.UserMapper;
import com.prd.model.LoginUser;
import com.prd.model.UserDO;
import com.prd.service.UserService;
import com.prd.util.CommonUtil;
import com.prd.util.JWTUtil;
import com.prd.util.JsonData;
import com.prd.vo.LoginRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author lizhaoyi
 * @date 2023/11/18 15:36
 * Description:
 */
@Service
@Slf4j
public class IDCartLoginRegisterStrategy implements LoginRegisterStrategy{

    @Autowired
    private UserMapper userMapper;


    /**
     * 身份证注册登录
     * @param loginRegisterVo
     * @return
     */
    @Override
    public JsonData LoginAndRegister(LoginRegisterVo loginRegisterVo) {

        if (StringUtils.isEmpty(loginRegisterVo.getPwd())) {
            return JsonData.buildError("身份证登录密码不能为空");
        }
        UserDO userDo = userMapper.selectOne(new LambdaQueryWrapper<UserDO>().eq(UserDO::getIdCardNum, loginRegisterVo.getIdCardNum()));
        if (userDo == null) {
            //注册
            UserDO userDO = register(loginRegisterVo);
            if (userDO != null) {
                return login(loginRegisterVo, userDO);
            }

        }else {
            //登录
            return login(loginRegisterVo, userDo);
        }
        return null;
    }

    private JsonData login(LoginRegisterVo loginRegisterVo, UserDO userDO) {
        //登录,校验密码
        String cryptPwd = Md5Crypt.md5Crypt(loginRegisterVo.getPwd().getBytes(), userDO.getSecret());
        if (cryptPwd.equals(userDO.getPwd())) {
            //登录成功,生成token
            LoginUser loginUser =  LoginUser.builder().build();
            BeanUtils.copyProperties(userDO, loginUser);
            String accessToken = JWTUtil.geneJsonWebToken(loginUser);
            return JsonData.buildSuccess(accessToken);
        } else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
        }
    }

    private UserDO register(LoginRegisterVo loginRegisterVo) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(loginRegisterVo, userDO);
        if (!StringUtils.isEmpty(loginRegisterVo.getPwd())) {
            //设置密码 生成秘钥 盐
            userDO.setSecret("$1$" + CommonUtil.getStringNumRandom(8));
            //密码+盐处理
            String cryptPwd = Md5Crypt.md5Crypt(loginRegisterVo.getPwd().getBytes(), userDO.getSecret());
            userDO.setPwd(cryptPwd);
        }
        userDO.setTelephone(null);
        userDO.setUnionId(null);
        userDO.setCreateTime(LocalDateTime.now());
        userDO.setType(LoginUserTypeEnum.ID_CART.getCode());
        int rows = userMapper.insert(userDO);
        log.info("rows:{},注册成功:{}", rows, userDO.toString());

        return rows>0?userDO:new UserDO();
    }

}
